package io.renren.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.exception.RRException;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.app.dao.BsEmployeeDao;
import io.renren.modules.app.dao.BsHealthexamDao;
import io.renren.modules.app.dao.BsMemberDao;
import io.renren.modules.app.entity.*;
import io.renren.modules.app.service.BsHealthexamService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;



@Service("bsHealthexamService")
@Slf4j
public class BsHealthexamServiceImpl extends ServiceImpl<BsHealthexamDao, BsHealthexamEntity> implements BsHealthexamService {

    @Resource
    private BsHealthexamDao bsHealthexamDao ;

    @Resource
    private BsMemberDao bsMemberDao ;

    @Resource
    private BsEmployeeDao bsEmployeeDao ;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        LambdaQueryWrapper<BsHealthexamEntity> wrapper = new LambdaQueryWrapper<>();

        if (params.get("memberName") != null && !params.get("memberName").toString().equals("")){
            LambdaQueryWrapper<BsMemberEntity> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.like(BsMemberEntity::getMemberName,params.get("memberName"));
            List<BsMemberEntity> bsMemberEntities = bsMemberDao.selectList(queryWrapper);
            List<String> list = new ArrayList<>();
            if (bsMemberEntities.size() > 0){
                for (BsMemberEntity bsMemberEntity : bsMemberEntities) {
                    list.add(bsMemberEntity.getMemberId());
                }
                wrapper.in(BsHealthexamEntity::getMemberId,list);
            }else {
                IPage<BsHealthexamEntity> page = this.page(
                        new Query<BsHealthexamEntity>().getPage(params),
                        new LambdaQueryWrapper<BsHealthexamEntity>().eq(BsHealthexamEntity::getId,-1)
                );
                return new PageUtils(page);
            }
        }
        if (params.get("examDateBegin") != null && !params.get("examDateBegin").equals("")){
            wrapper.ge(BsHealthexamEntity::getExamDate,params.get("examDateBegin"));
        }
        if (params.get("examDateEnd") != null && !params.get("examDateEnd").equals("")){
            wrapper.le(BsHealthexamEntity::getExamDate,params.get("examDateEnd"));
        }
        IPage<BsHealthexamEntity> page = this.page(
                new Query<BsHealthexamEntity>().getPage(params),
                wrapper
        );
        for (BsHealthexamEntity record : page.getRecords()) {
            //会员姓名数据清洗
            String memberId = record.getMemberId();
            List<BsMemberEntity> entities = bsMemberDao.selectList(
                    new LambdaQueryWrapper<BsMemberEntity>()
                            .eq(BsMemberEntity::getMemberId, memberId));
            record.setMemberName(entities.get(0).getMemberName());
            //巡检人员数据清洗
            String examEmployid = record.getExamEmployid();
            List<BsEmployeeEntity> entities1 = bsEmployeeDao.selectList(
                    new LambdaQueryWrapper<BsEmployeeEntity>()
                            .eq(BsEmployeeEntity::getEmployeeId, examEmployid));
            record.setExamEmployName(entities1.get(0).getEmployeeName());
        }

        return new PageUtils(page);
    }

    @Override
    public void saveHealth(BsHealthexamEntity bsHealthexam) {
        try {
            //会员编号存在性校验
            List<BsMemberEntity> entities = bsMemberDao.selectList(
                    new LambdaQueryWrapper<BsMemberEntity>()
                            .eq(BsMemberEntity::getMemberId, bsHealthexam.getMemberId()));
            if (entities.size() < 1){
                throw new RRException("请校验会员编号合法性");
            }
            //巡检人员存在性校验
            List<BsEmployeeEntity> entities1 = bsEmployeeDao.selectList(
                    new LambdaQueryWrapper<BsEmployeeEntity>()
                            .eq(BsEmployeeEntity::getEmployeeId, bsHealthexam.getExamEmployid()));
            if (entities1.size() < 1){
                throw new RRException("请校验巡检人员编号合法性");
            }
            bsHealthexamDao.insert(bsHealthexam);
        }catch (Exception e){
            log.info("报错类及其所在方法：{}","BsHealthexamService --> saveHealth");
            log.info(e.getMessage());
            throw new RRException(e.getMessage());
        }
    }

    @Override
    public void updateHealth(BsHealthexamEntity bsHealthexam) {
        try {
            //该修改不支持会员编号修改，
            String memberId = bsHealthexamDao.selectById(bsHealthexam.getId()).getMemberId();
            if (!memberId.equals(bsHealthexam.getMemberId())){
                throw new RRException("会员编号不可修改");
            }
            //巡检人员存在性校验
            List<BsEmployeeEntity> entities1 = bsEmployeeDao.selectList(
                    new LambdaQueryWrapper<BsEmployeeEntity>()
                            .eq(BsEmployeeEntity::getEmployeeId, bsHealthexam.getExamEmployid()));
            if (entities1.size() < 1){
                throw new RRException("请校验巡检人员编号合法性");
            }
            bsHealthexamDao.updateById(bsHealthexam);
        }catch (Exception e){
            log.info("报错类及其所在方法：{}","BsHealthexamService --> updateHealth");
            log.info(e.getMessage());
            throw new RRException(e.getMessage());
        }
    }

}
